package com.cf.base.sp.engine.impl;

import com.cf.base.sp.annotation.Level;
import com.cf.base.sp.engine.Storage;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yangji
 */
public class MemoryStorageImpl implements Storage {

    private Map<Level, Map<String, String>> stringMap = new ConcurrentHashMap<>();

    private Map<String, String> getMap(Level level) {
        if (stringMap.get(level) == null) {
            stringMap.put(level, new HashMap<>());
        }
        return stringMap.get(level);
    }

    @Override
    public boolean put(String key, String t, Level level) {
        getMap(level).put(key, t);
        return true;
    }

    @Override
    public String get(String key, Level level) {
        return getMap(level).get(key);
    }

    @Override
    public int count(Level level) {
        return getMap(level).size();
    }

    @Override
    public boolean contains(String key, Level level) {
        return getMap(level).containsKey(key);
    }

    @Override
    public void delete(String key, Level level) {
        getMap(level).remove(key);
    }

    @Override
    public void clear(Level level) {
        getMap(level).clear();
    }
}
